<template>
    <div>
        <el-page-header @back="gotoRoute('/eva-project/pager')" content="领导人员设置">
        </el-page-header>
        <el-divider></el-divider>
        <el-button-group v-if="allowed([0])">
            <el-button :disabled="is_finished" size="mini" type="primary" @click="handEdit">新增</el-button>
        </el-button-group>
        <el-divider></el-divider>
        <el-table :data="tableData.rows" border class="w-80%">
            <el-table-column label="排序" width="220">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button size="mini" type="info" @click="deltaSort(scope.row.id, scope.row.sort, -1)"><i
                                class="fa fa-arrow-up"></i> </el-button>
                        <el-button size="mini" type="warning" @click="deltaSort(scope.row.id, scope.row.sort, 1)"><i
                                class="fa fa-arrow-down" aria-hidden="true"></i></el-button>
                    </el-button-group>
                </template>
            </el-table-column>
            <el-table-column label="顺序" width="50">
                <template slot-scope="scope">
                    <el-tag type="info">{{ scope.row.sort + 1 }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="名称" width="100">
            </el-table-column>
            <el-table-column prop="unique_id" label="标识" width="100">
            </el-table-column>
            <el-table-column prop="category" label="类别" width="120">
            </el-table-column>
            <el-table-column prop="type_name" label="人员类型" width="120">
            </el-table-column>
            <el-table-column prop="role_name" label="职务" width="120">
            </el-table-column>
            <el-table-column prop="bod" label="出生年月" width="120">
            </el-table-column>
            <el-table-column prop="remark" label="备注" width="220">
            </el-table-column>
            <el-table-column v-if="allowed([0])" fixed="right" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button :disabled="is_finished" @click="handEdit(scope.row)" type="success"
                        size="small">编辑</el-button>
                    <el-button :disabled="is_finished" @click="handRemove(scope.row)" type="danger"
                        size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @current-change="handPage" layout="prev, pager, next" :total="tableData.count"
            :page-size="tableData.offset">
        </el-pagination>
    </div>
</template>

<script>
import { FormDialog } from '@formily/element'
import { InputForm } from './InputForm';
import { BaseApi } from '../../../api/common/base-api';
import { sharedConfig } from '../../shared/BaseCurd';
import { onFieldValueChange } from '@formily/core';
import { onFieldInfo } from '../../../api/common/field-event';
const api = new BaseApi({ prefix: "/eva-project-leader" });
const entity = {
    project_id: "",
    name: "",
    unique_id: "",
    category: "",
    type_id: "",
    type_name: "",
    role_name: "",
    bod: "",
    remark: "",
}
export default {
    props: ["project_id", "is_finished"],
    name: "EvaProjectLeaderPager",
    async mounted() {
        this.loadPager(1);
    },
    methods: {
        ...sharedConfig.methods,
        async loadPager({ page }) {
            const data = await api.findPager({ page, where: { project_id: this.project_id }, order: [['sort']] });
            this.tableData = { ...data.data };
        },
        handEdit(row) {
            FormDialog('编辑项目领导人员', InputForm)
                .forOpen((payload, next) => {
                    const raw = (row instanceof PointerEvent) ? entity : row;
                    raw.project_id = this.project_id;
                    next({
                        initialValues: { ...raw },
                        effects() {
                            onFieldValueChange('type_info', (field, form) => {
                                const names = ["type_id", "type_name"];
                                onFieldInfo({ names, field, form });
                            })
                        }
                    })
                })
                .forConfirm((payload, next) => {
                    next(payload)
                })
                .forCancel((payload, next) => {
                    next(payload)
                })
                .open()
                .then(async (values) => {
                    Reflect.deleteProperty(values, "type_info")
                    await this._save({ api, values })
                    await this.loadPager(1);
                })
                .catch(console.error)
        },
        handRemove(row) {
            this.$confirm("确定要删除?", "系统提示").then(async () => {
                await this._remove({ api, where: { id: row.id } });
                await this.loadPager(1);
            }).catch(console.error);

        },
        async deltaSort(id, sort, delta) {
            await this.sort(api, id, sort, delta)
        }
    },

    data() {
        return {
            tableData: {
                count: 0,
                offset: 10,
                page: 1,
                rows: [],
            }
        }
    }
}
</script>